package com.jy.admin.rest;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.jy.admin.db.dao.ClUploadDao;
import com.jy.admin.db.entity.ClUploadEntity;
import com.jy.admin.rest.request.ClUploadRequest;
import com.jy.admin.rest.response.ClListResponse;
import com.jy.admin.rest.response.ProductListResponse;
import com.jy.admin.rest.response.ProductSearchRequest;
import com.jy.bo.common.conf.api.ApiResponse;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.robert.vesta.util.IdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/11/23
 * Time: 3:04 下午
 */

@RestController
@RequestMapping("/cl")
public class ClController {

    @Autowired
    private ClUploadDao clUploadDao;

    @RequestMapping(value = "/add")
    public ApiResponse<Boolean> add(@RequestBody ClUploadRequest request) {
        if (StringUtils.isEmpty(request.getRecordCode())) {
            request.setRecordCode(IdUtils.genId() + "");
            ClUploadEntity entity = new ClUploadEntity();
            BeanUtils.copyProperties(request, entity);
            clUploadDao.insert(entity);
            return new ApiResponse<Boolean>(true);
        }

        ClUploadEntity byCode = clUploadDao.findByCode(request.getRecordCode());
        if (byCode == null) {
            throw new ApplicationException("record not exist");
        }

        BeanUtils.copyProperties(request, byCode);
        clUploadDao.updateByPrimaryKey(byCode);
        return new ApiResponse<Boolean>(true);
    }

    @RequestMapping(value = "/list/{adminCode}/{status}")
    public ApiResponse<ClListResponse> list(@PathVariable String adminCode,@PathVariable Integer status) {
        List<ClUploadEntity> listByAdminAndStatus = clUploadDao.findListByAdminAndStatus(adminCode, status);
        return new ApiResponse<ClListResponse>(new ClListResponse(listByAdminAndStatus));
    }


    @RequestMapping(value = "/list/{status}/all")
    public ApiResponse<ClListResponse> all(@PathVariable Integer status) {
        List<ClUploadEntity> listByStatus = clUploadDao.findListByStatus(status);
        return new ApiResponse<ClListResponse>(new ClListResponse(listByStatus));
    }


}
